<?php

namespace app\admin\model\deerhome;

use think\Model;
use app\admin\model\deerhome\Items;

class Shop extends Model
{

    // 表名
    protected $name = 'deerhome_shop';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'open_txt'
    ];
    protected static function init()
    {
        self::beforeInsert(function ($row) {
            $row->reg_time=date('Y-m-d H:i:s');
            $address=explode('/',$row->address_face);
            if(count($address)!=3){
                throw new \Exception('请选择所在区域');
            }
            $row->address1 = $address[0];
            $row->address2 = $address[1];
            $row->address3 = $address[2];
        });
        self::beforeWrite(function ($row) {
            if(empty($row->mdname)){
                throw new \Exception(__('门店名称不能为空'));
            }
            if(empty($row->latitude) || empty($row->longitude) || empty($row->address)){
                throw new \Exception(__('地址和经纬度不能为空'));
            }
            $address=explode('/',$row->address_face);
            if(count($address)!=3){
                throw new \Exception('请选择所在区域');
            }
            $row->address1 = $address[0];
            $row->address2 = $address[1];
            $row->address3 = $address[2];
        });
    }

    public function getOpenTxtAttr($value, $data){
        if(isset($data['open_time']) && isset($data['close_time'])){
            return date("H:i",strtotime($data['open_time']))."-".date("H:i",strtotime($data['close_time']));
        }
        return '';
    }
    public function getLogoImagesAttr($value, $data){
        if(!$data['logo_images']){
            return '';
        }
        return cdnurl($data['logo_images'],true);
    }
    public function getTypeList(){
        return ['1' => "自营", '2' => "入驻"];
    }
    //获取某个产品支持的门店列表
    public function getShopListByItemsId($item_id,$latitude="",$longitude=""){
        $items=Items::get($item_id);
        if($items['shop_in']==1){
            return [];
        }
        $where=[];
        $where["status"]=1;
        //指定
        if($items['shop_way']==1){
            $shopList=\json_decode($items['shop_list'],true);
            $shopListIds=[];
            foreach($shopList as $v){
                $shopListIds[]=$v['id'];
            }
            $where["id"]=["in",$shopListIds];
        }
        //自营
        if($items['shop_way']==2){
            $where["type"]=1;
        }
        $field="id,type,logo_images,mdname,address,ROUND(ROUND(6378.138*2*ASIN(SQRT(POW(SIN(({$latitude}*PI()/180-latitude*PI()/180)/2),2)+COS({$latitude}*PI()/180)*COS(latitude*PI()/180)*POW(SIN(({$longitude}*PI()/180-longitude*PI()/180)/2),2)))*1000)/1000,1) AS km";
        $res=self::field($field)->where($where)->order('km asc')->limit(30)->select();
        return $res;
    }
    public function Worker(){
        return $this->hasMany('app\admin\model\deerhome\Worker','jz_shop_id','id') ;
    }

    public function user()
    {
        return $this->belongsTo('app\admin\model\deerhome\User', 'deerhome_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }



}
